import router from './router'
import store from '@/store'
import { getToken } from '@/utils/auth'
import website from './plugins/config/website'

let asyncRouterFlag = 0
const whiteList = ['/login', '/404', '/400']

router.beforeEach((to, from, next) => {
  const token = getToken()
  document.title = to.meta.title ? to.meta.title : website.title
  if (token) {
    if (to.path === '/login') {
      next('/login')
    } else {
      if (!asyncRouterFlag) {
        asyncRouterFlag = asyncRouterFlag + 1
        store.dispatch("permission/getUserMenu")
      }
      next()
    }
  } else {
    // 没有token
    if (whiteList.indexOf(to.path) !== -1) {
      // 在免登录白名单，直接进入
      next()
    } else {
      next('/login') // 否则全部重定向到登录页
    }
  }
})

